package com.csit.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * User: xpk
 * Date: 12-8-4
 * Time: 下午9:56
 */
public class ConfigHelper extends Properties {
	
	private static final long serialVersionUID = 2826925335457455391L;
	
	private String[] locations;

	public int getInt(String key, int defValue) {
		Object o = get(key);
		return o == null ? defValue : toInt(o, defValue);
	}

	public String getString(String key, String defValue) {
		Object o = get(key);
		if (o == null)
			return defValue;
		return o.toString();
	}

	public boolean getBoolean(String key, boolean defValue) {
		Object o = get(key);
		if (o == null)
			return defValue;
		if (o instanceof Boolean)
			return (Boolean) o;
		return "true".equals(o) || "1".equals(o);
	}

	public long getLong(String key, long defValue) {
		Object o = get(key);
		return o == null ? defValue : toLong(o, defValue);
	}

	public float getFloat(String key, float defValue) {
		Object o = get(key);
		return o == null ? defValue : toFloat(o, defValue);
	}

	public double getDouble(String key, double defValue) {
		Object o = get(key);
		return o == null ? defValue : toDouble(o, defValue);
	}

	private static int toInt(Object obj, int defValue) {
		if (obj == null)
			return defValue;
		if (obj instanceof Integer)
			return (Integer) obj;
		try {
			return Integer.parseInt(obj.toString());
		} catch (Throwable e) {
			return defValue;
		}
	}

	private static float toFloat(Object obj, float defValue) {
		if (obj == null)
			return defValue;
		if (obj instanceof Integer)
			return (Integer) obj;
		if (obj instanceof Long)
			return (Long) obj;
		if (obj instanceof Double)
			return ((Double) obj).floatValue();
		try {
			return Float.parseFloat(obj.toString());
		} catch (Throwable e) {
			return defValue;
		}
	}


	private static double toDouble(Object obj, double defValue) {
		if (obj == null)
			return defValue;
		if (obj instanceof Integer)
			return (Integer) obj;
		if (obj instanceof Long)
			return (Long) obj;
		if (obj instanceof Float)
			return (Float) obj;
		if (obj instanceof Double)
			return (Double) obj;
		try {
			return Double.parseDouble(obj.toString());
		} catch (Throwable e) {
			return defValue;
		}
	}

	private static long toLong(Object obj, long defValue) {
		if (obj == null)
			return defValue;
		if (obj instanceof Integer)
			return (Integer) obj;
		if (obj instanceof Long)
			return (Long) obj;
		try {
			return Long.parseLong(obj.toString());
		} catch (Throwable e) {
			return defValue;
		}
	}

	public Properties getProperties() {
		return this;
	}

	public void setProperties(Properties properties) {
		this.clear();
		if (properties != null)
			this.putAll(properties);
	}

	public void setLocations(String[] locations) {
		this.locations = locations;
		init();

	}

	private void init() {
		if (this.locations != null && this.locations.length > 0) {
			for (String location : locations) {
				if (location.startsWith("classpath*:")) {
					loadFromClassPath(location.substring("classpath*:".length()));
				} else {
					loadFromUrl(location);
				}
			}
		}
	}

	private void loadFromUrl(String location) {

		Properties props = new Properties();
		InputStream is = null;
		try {
			is = new URL(location).openStream();
			props.load(is);
			this.putAll(props);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null)
					is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void loadFromClassPath(String res) {
		Properties props = new Properties();
		InputStream is;
		is = getClass().getClassLoader().getResourceAsStream(res);
		if (is != null)
			try {
				props.load(is);
				this.putAll(props);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	}

}
